#include <stdio.h>
#include <assert.h>

#include "AstNodeLiteral.h"
#include "AstNodeExpression.h"
#include "AstNodePrintStatement.h"

#include <CompileVisitor.h>

#if 0
int AstNodePrintLiteral::compile(ClassFile * klass, MethodInfo * method)
{
  u2 constantFieldRef=klass->addFieldRef(
      "java/lang/System","out", "Ljava/io/PrintStream;");

  u2 constantMethodRef;

  if(endLine)
    constantMethodRef=klass->addMethodRef(
        "java/io/PrintStream","println", "(Ljava/lang/String;)V");
  else
    constantMethodRef=klass->addMethodRef(
        "java/io/PrintStream","print", "(Ljava/lang/String;)V");

  method->assemble(OPC_getstatic,constantFieldRef);
  literal->compile(klass,method);
  method->assemble(OPC_invokevirtual,constantMethodRef);

  return 0;
}
#endif

void AstNodePrintLiteral::compile(CompileVisitor * visitor)
{
  visitor->compile(this);
}


AstNodePrintLiteral::AstNodePrintLiteral(AstNodeLiteral * son, bool endLine)
{
  this->literal=son;
  this->endLine=endLine;
}

void AstNodePrintLiteral::dump(int level)
{
  printf("print ");
  literal->dump(level+1);
  printf("\n");
}

AstNodePrintExpression::AstNodePrintExpression(AstNodeExpression * expr, bool endLine)
{
  this->expr=expr;
  this->endLine=endLine;
}

void AstNodePrintExpression::compile(CompileVisitor * visitor)
{
  visitor->compile(this);
}

#if 0
int AstNodePrintExpression::compile(ClassFile * klass, MethodInfo * method)
{
  u2 constantFieldRef=klass->addFieldRef(
      "java/lang/System","out", "Ljava/io/PrintStream;");

  u2 constantMethodRef;

  if(endLine)
    constantMethodRef=klass->addMethodRef(
        "java/io/PrintStream","println", "(D)V");
  else
    constantMethodRef=klass->addMethodRef(
        "java/io/PrintStream","print", "(D)V");

  method->assemble(OPC_getstatic,constantFieldRef);
  expr->compile(klass,method);
  method->assemble(OPC_invokevirtual,constantMethodRef);

  return 0;
}
#endif

void AstNodePrintExpression::dump(int level)
{
  printf("printExpression ");
  expr->dump(0);
  printf("\n");
}
